{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Definition for a binary tree node.\n",
    "# class TreeNode:\n",
    "#     def __init__(self, x):\n",
    "#         self.val = x\n",
    "#         self.left = None\n",
    "#         self.right = None\n",
    "\n",
    "class Solution:\n",
    "    def getMinimumDifference(self, root) -> int:\n",
    "        \n",
    "        def dfs(node):\n",
    "            if node is None:\n",
    "                return\n",
    "            \n",
    "            vals.append(node.val)\n",
    "            dfs(node.left)\n",
    "            dfs(node.right)\n",
    "\n",
    "            return\n",
    "        \n",
    "        vals = []            \n",
    "        dfs(root)\n",
    "        \n",
    "        vals.sort()        \n",
    "        min_diff = 2**31\n",
    "        \n",
    "        for i in range(len(vals)-1):\n",
    "            diff = vals[i+1] - vals[i]\n",
    "            if diff < min_diff:\n",
    "                min_diff = diff\n",
    "\n",
    "        return min_diff"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
